package com.manor.paradise.activity

import android.content.Context
import android.content.Intent
import android.text.InputFilter
import android.text.TextUtils
import cn.qqtheme.framework.entity.Province
import cn.qqtheme.framework.picker.AddressPicker
import com.bumptech.glide.Glide
import com.manor.common.CommonAppConfig
import com.manor.common.Constants
import com.manor.common.activity.AbsActivity
import com.manor.common.http.JHttpCallback
import com.manor.common.interfaces.CommonCallback
import com.manor.common.utils.CityUtil
import com.manor.common.utils.DialogUitl
import com.manor.common.utils.KLog
import com.manor.common.utils.ToastUtil
import com.manor.paradise.R
import com.manor.paradise.bean.AddressBean
import com.manor.paradise.http.ElvesHttpConst
import com.manor.paradise.http.ElvesHttpUtil
import kotlinx.android.synthetic.main.activity_addaddress.*
import java.util.*

/**
 * 添加收货地址
 */
class AddAddressActivity : AbsActivity() {

    var mAreaCode: String = ""
    var mCityCode: String = ""
    var mProvinceCode: String = ""
    var mProvinceVal: String = ""
    var mCityVal: String = ""
    var mZoneVal: String = ""
    var data: AddressBean.RecordsBean? = null;

    override fun getLayoutId(): Int {
        return R.layout.activity_addaddress
    }

    companion object {
        fun forward(context: Context, item: AddressBean.RecordsBean) {
            val intent = Intent(context, AddAddressActivity::class.java)
            intent.putExtra(Constants.ED_ADDRESS, item)
            context.startActivity(intent)
        }
    }

    override fun main() {
        super.main()
        if (null != intent && null != intent.getSerializableExtra(Constants.ED_ADDRESS)) {//如果等于null 就是新建收货地址 反  修改收货地址
            data = intent.getSerializableExtra(Constants.ED_ADDRESS) as AddressBean.RecordsBean
            data?.run {
                ed_name.setText(  contacts)
                ed_tel.setText(  mobile)
                tv_city.text = provinceName +   cityName +   areaName
                ed_address.setText(  address)

                mAreaCode =   areaCode
                mZoneVal =   areaName
                mCityCode =   cityCode
                mCityVal =   cityName
                mProvinceCode =   provinceCode
                mProvinceVal = data?.provinceName?:""
                im_title.setText("修改收货地址")
            }

        }
        ed_tel.setFilters(arrayOf<InputFilter>(InputFilter.LengthFilter(11))) //最大输入长度
        im_back.setOnClickListener({ finish() })
        ll_city.setOnClickListener({
            chooseCity()
        })
        tv_confirm.setOnClickListener {
            if (data != null) {
                if (!TextUtils.isEmpty(ed_name.text.toString()) && !TextUtils.isEmpty(ed_tel.text.toString()) && !TextUtils.isEmpty(ed_address.text.toString()) && !tv_city.text.toString().equals("")) {
                    ElvesHttpUtil.getEdlogistics(
                            data?.id ?: "",
                            ed_address.text.toString(),
                            mAreaCode,
                            mZoneVal,
                            mCityCode,
                            mCityVal,
                            ed_name.text.toString(),
                            ed_tel.text.toString(),
                            mProvinceCode,
                            mProvinceVal, object : JHttpCallback() {
                        override fun onSuccess(code: Int, msg: String, info: String) {

                            if (code != 200) {
                                ToastUtil.show(msg)
                                return
                            }

                            finish()
                        }

                        override fun onError() {
                            super.onError()

                        }
                    })
                } else {
                    if (TextUtils.isEmpty(ed_name.text.toString())) {
                        ToastUtil.show("请输入收货人姓名")
                    }
                    if (TextUtils.isEmpty(ed_tel.text.toString())) {
                        ToastUtil.show("请输入联系电话")
                    }
                    if (TextUtils.isEmpty(ed_address.text.toString())) {
                        ToastUtil.show("请输入详细地址")
                    }
                    if (tv_city.text.toString().equals("")) {
                        ToastUtil.show("请选择位置")
                    }
                }
                return@setOnClickListener
            }
            if (!TextUtils.isEmpty(ed_name.text.toString()) && !TextUtils.isEmpty(ed_tel.text.toString()) && !TextUtils.isEmpty(ed_address.text.toString()) && !tv_city.text.equals("")) {
                ElvesHttpUtil.getAddrlogistics(ed_address.text.toString(),
                        mAreaCode,
                        mZoneVal,
                        mCityCode,
                        mCityVal,
                        ed_name.text.toString(),
                        ed_tel.text.toString(),
                        mProvinceCode,
                        mProvinceVal, object : JHttpCallback() {
                    override fun onSuccess(code: Int, msg: String, info: String) {

                        if (code != 200) {
                            ToastUtil.show(msg)
                            return
                        }

                        finish()
//                        var addressBean: AddressBean = GsonUtil.gsonToBean<AddressBean>(info, AddressBean::class.java)
//                        KLog.e("json", addressBean);


                    }

                    override fun onError() {
                        super.onError()

                    }
                })
            }
        }
    }

    /**
     * 省市区三级联动
     */
    private fun chooseCity() {
        val list = CityUtil.getInstance().cityList
        if (list == null || list.size == 0) {
            val loading = DialogUitl.loadingDialog(this)
            loading.show()
            CityUtil.getInstance().getCityListFromAssets(object : CommonCallback<ArrayList<Province>>() {
                override fun callback(newList: ArrayList<Province>?) {
                    loading.dismiss()
                    if (newList != null) {
                        showChooseCityDialog(newList)
                    }
                }
            })
        } else {
            showChooseCityDialog(list)
        }
    }


    /**
     * 选择城市
     */
    private fun showChooseCityDialog(list: ArrayList<Province>?) {
        var province = mProvinceVal
        var city = mCityVal
        var district = mZoneVal
        if (TextUtils.isEmpty(province)) {
            province = CommonAppConfig.getInstance().province
        }
        if (TextUtils.isEmpty(city)) {
            city = CommonAppConfig.getInstance().city
        }
        if (TextUtils.isEmpty(district)) {
            district = CommonAppConfig.getInstance().district
        }
        DialogUitl.showCityChooseDialog(this, list, province, city, district, AddressPicker.OnAddressPickListener { province, city, county ->
            val provinceName = province.areaName
            val cityName = city.areaName
            val zoneName = county.areaName

            mAreaCode = county.areaId
            mCityCode = city.areaId
            mProvinceCode = province.areaId


            mProvinceVal = provinceName
            mCityVal = cityName
            mZoneVal = zoneName
            KLog.e("city", provinceName + cityName + zoneName)
            tv_city.setText(provinceName + cityName + zoneName)

        })
    }

    override fun onDestroy() {
        Glide.get(this).clearMemory()
        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_ADDR_UP)

        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_ADDR_ADD)
        super.onDestroy()
    }
}
